package com.example.demo.utils;


import com.example.demo.entity.ResultEnum;

import java.util.*;

/**
 * 功能描述: 返回结果封装工具类
 *
 * @date 2018/9/14 22:24
 */
public class Waiter {
    private final static String RES_CODE = "resCode";
    private final static String MSG = "msg";
    private final static String OBJ = "obj";
    private final static Map<ResultEnum, Map<Object, Object>> RESPONSE_RESULT_MAP = new HashMap<>();

    static {
        for (ResultEnum resultEnum : EnumSet.allOf(ResultEnum.class)) {
            RESPONSE_RESULT_MAP.put(resultEnum, new HashMap<Object, Object>() {
                {
                    put(RES_CODE, resultEnum.getCode());
                    put(MSG, resultEnum.getMsg());
                }
            });
        }
    }

    /**
     * 功能描述: 出菜失败了
     *
     * @param resultEnum 结果枚举
     * @return 失败原因
     * @date 2018/9/14
     */
    public static Map<Object, Object> error(ResultEnum resultEnum) {
        return RESPONSE_RESULT_MAP.get(resultEnum);
    }

    /**
     * 功能描述: 出菜成功了
     *
     * @param o       请求结果对象
     * @param objName 请求结果名称
     * @return 包装好的菜
     * @date 2018/9/14 22:01
     */
    public static Map<Object, Object> success(Object o, String objName) {
        Map<Object, Object> resMap = new HashMap<>(RESPONSE_RESULT_MAP.get(ResultEnum.SUCCESS));
        if (objName == null || objName.equals("")) {
            if (o == null) {
                resMap.put(OBJ, new HashMap<>());
            } else {
                resMap.put(OBJ, o);
            }
        } else {
            Map<Object, Object> map = new HashMap<>();
            map.put(objName, o);
            resMap.put(OBJ, map);
        }
        return resMap;
    }
}
